package org.soderiakelvy.controller.producto;

import java.util.List;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.soderiakelvy.controller.util.Constante;
import org.soderiakelvy.controller.util.SelectOneMany;
import org.soderiakelvy.model.domain.PackVO;
import org.soderiakelvy.model.service.ProductoServicio;
import org.soderiakelvy.model.service.imp.ProductoServicioImp;
import org.soderiakelvy.model.util.ConstantModel;


@ManagedBean
public class PackList {
	private List<PackVO> listadoDePack =  SelectOneMany.getPacks();
	private ProductoServicio service = new ProductoServicioImp();
	private PackVO pack = new PackVO();
	
	public PackList(){
		listadoDePack =  SelectOneMany.getPacks();
		System.out.println("Constructor de PACKLIST");
	}
	
	public String nuevoPack(){
		return Constante.URL_PACK_NEW;
	}

	public String eliminarPack(){
		String result = this.service.deletePackVO(pack);
		if(result.equals(ConstantModel.SUCCESS)){
			listadoDePack = SelectOneMany.getPacks();
			return Constante.URL_PACK_LIST;
		}else{
			if(result.equals(ConstantModel.DELETE_ASIGNADO)){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"No se puede eliminar el Pack porque tiene asignado una Comercializacion", "sss"));
				return null;
			}else {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Error al Insertar un registro", "Surgio un error al eliminar un registro. Comuicarse con el responsable del Sistema"));
				return null;
			}
		}
	}

	public void bajaListener(PackVO p){
		this.pack = p;
 		   System.out.println("presiono baja  "+ p.getNombre());
	}

	
	public String prepareEdit(PackVO pack){
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String,Object> sessionParams = fc.getExternalContext().getSessionMap();
		sessionParams.put("pack", pack);

		return Constante.URL_PACK_EDIT;
	}
	// TODO Get and Set
	public List<PackVO> getListadoDePack() {
		return listadoDePack;
	}

	public PackVO getPack() {
		return pack;
	}

	public void setPack(PackVO pack) {
		this.pack = pack;
	}

}
